package com.ruoyi.binding.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.binding.domain.MhUserGamePrice;
import com.ruoyi.binding.mapper.MhBindingMapper;
import com.ruoyi.binding.domain.MhBinding;
import com.ruoyi.binding.service.IMhBindingService;

/**
 * 工作室改价比例Service业务层处理
 *
 * @author Ekko
 * @date 2025-06-24
 */
@Slf4j
@Service

public class MhBindingServiceImpl implements IMhBindingService {
    @Autowired
    private MhBindingMapper mhBindingMapper;
    @Autowired
    private MhGameCategoryServiceImpl mhGameCategoryServiceImpl;

    @DataScope(userAlias = "u", deptAlias = "d")
    private List<MhUserGamePrice> selectMhUserGamePriceListByBindingId(Long bindingId) {
        return mhBindingMapper.selectMhUserGamePriceListByBindingId(bindingId);
    }

    /**
     * 查询工作室改价比例
     *
     * @param id 工作室改价比例主键
     * @return 工作室改价比例
     */
    @DataScope(userAlias = "u", deptAlias = "d")
    @Override
    public MhBinding selectMhBindingById(Long id) {
        MhBinding mhBinding;
//        根据id查bindingId再传入
        mhBinding = mhBindingMapper.selectMhBindingById(id);
        mhBinding.setMhUserGamePriceList(selectMhUserGamePriceListByBindingId(mhBinding.getId()));
        return mhBinding;
    }

    /**
     * 查询工作室改价比例列表
     *
     * @param mhBinding 工作室改价比例
     * @return 工作室改价比例
     */
    @Override
    @DataScope(userAlias = "u", deptAlias = "d")
    public List<MhBinding> selectMhBindingList(MhBinding mhBinding) {
        return mhBindingMapper.selectMhBindingList(mhBinding);
    }

    /**
     * 新增工作室改价比例
     *
     * @param mhBinding 工作室改价比例
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMhBinding(MhBinding mhBinding) {
        int rows = 0;
        mhBinding.setAdminId(SecurityUtils.getUserId());
        for (Long userId : mhBinding.getUserIds()) {
            mhBinding.setUserId(userId);
            rows += mhBindingMapper.insertMhBinding(mhBinding);
        }
//        insertMhUserGamePrice(mhBinding);
        return rows;
    }

    /**
     * 修改工作室改价比例
     *
     * @param mhBinding 工作室改价比例
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMhBinding(MhBinding mhBinding) {
        mhBindingMapper.deleteMhUserGamePriceByBindingId(mhBinding.getId());
        insertMhUserGamePrice(mhBinding);
        return mhBindingMapper.updateMhBinding(mhBinding);
    }

    /**
     * 批量删除工作室改价比例
     *
     * @param ids 需要删除的工作室改价比例主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMhBindingByIds(Long[] ids) {
        mhBindingMapper.deleteMhUserGamePriceByBindingIds(ids);
        return mhBindingMapper.deleteMhBindingByIds(ids);
    }

    /**
     * 删除工作室改价比例信息
     *
     * @param id 工作室改价比例主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMhBindingById(Long id) {
        mhBindingMapper.deleteMhUserGamePriceByBindingId(id);
        return mhBindingMapper.deleteMhBindingById(id);
    }

    /**
     * @param userId
     * @return
     */
    @Override
    public MhBinding selectMhBindingByUserId(Long userId) {
       return mhBindingMapper.selectMhBindingByUserId(userId);
    }

    /**
     * @param adminId
     * @return
     */
    @Override
    public List<MhBinding> selectMhBindingByAdminId(Long adminId) {
        return mhBindingMapper.selectMhBindingByAdminId(adminId);
    }

    /**
     * @param adminId
     * @param userId
     * @return
     */
    @Override
    public MhBinding selectMhBindingByAdminIdAndUserId(Long adminId, Long userId) {
        return mhBindingMapper.selectMhBindingByAdminIdAndUserId(adminId, userId);
    }

    /**
     * 新增扣款比例信息
     *
     * @param mhBinding 工作室改价比例对象
     */


    public void insertMhUserGamePrice(MhBinding mhBinding) {
        List<MhUserGamePrice> mhUserGamePriceList = mhBinding.getMhUserGamePriceList();
        Long id = mhBinding.getId();
        if (StringUtils.isNotNull(mhUserGamePriceList)) {
            List<MhUserGamePrice> list = new ArrayList<MhUserGamePrice>();
            for (MhUserGamePrice mhUserGamePrice : mhUserGamePriceList) {
                mhUserGamePrice.setBindingId(id);
                log.info("游戏类别ID:{}", mhUserGamePrice.getGameCategoryName());
                mhUserGamePrice.setGameCategoryId(mhGameCategoryServiceImpl.selectMhGameCategoryByCategoryName(mhUserGamePrice.getGameCategoryName()).getId());
                list.add(mhUserGamePrice);
            }
            if (list.size() > 0) {
                mhBindingMapper.batchMhUserGamePrice(list);
            }
        }
    }


}
